package danny.hoi.web.controller.admin;


import com.github.pagehelper.PageInfo;
import danny.hoi.domain.export.admin.Module;
import danny.hoi.domain.export.admin.ModuleExample;
import danny.hoi.service.export.admin.ModuleService;
import danny.hoi.service.export.system.DeptService;
import danny.hoi.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;
    @Autowired
    private DeptService deptService;

    /**
     * 1.分页显示企业信息
     * 2.请求地址:module/list.do
     * 3.响应地址:WEB-INF/pages/module/module-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String key) {
        if (key == null) {
            key = "";
        }
        ModuleExample example = new ModuleExample();
        ModuleExample.Criteria criteria = example.createCriteria();
        //设置条件查询
        criteria.andNameLike("%" + key + "%");
        PageInfo<Module> pageInfo = moduleService.findByPages(example, pageNum, pageSize);


        request.setAttribute("key", key);
        request.setAttribute("pageInfo", pageInfo);

        return "system/module/module-list";
    }

    /**
     * 1.前往添加页面
     * 2.请求地址:/module/toAdd.do
     * 3.响应地址:WEB-INF/pages/module/module-add.jsp
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        //查出所有的模块
        ModuleExample example = new ModuleExample();

        List<Module> moduleList = moduleService.findAll(example);
        request.setAttribute("menus", moduleList);

        return "system/module/module-add";
    }

    /**
     * 1.前往修改页面
     * 2.请求地址:/module/toUpdate.do
     * 3.响应地址:WEB-INF/pages/module/module-update.jsp
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        Module module = moduleService.findById(id);
        request.setAttribute("module", module);
        //查出所有的第一和第二级模块
        ModuleExample example = new ModuleExample();

        List<Module> moduleList = moduleService.findAll(example);
        request.setAttribute("menus", moduleList);
        return "system/module/module-update";
    }


    /**
     * 1.更新页面
     * 2.请求地址:/module/edit.do
     * 3.响应地址:redirect:/module/list.do
     */
    @RequestMapping("/edit")
    public String edit(Module module) {
        if ("".equals(module.getModuleId())) {
            module.setModuleId(null);
        }
        if (StringUtils.isEmpty(module.getModuleId())) {
            //添加
            moduleService.save(module);
        } else {
            //更新
            moduleService.update(module);
        }

        return "redirect:/system/module/list.do";
    }

    /**
     * 1.删除
     * 2.请求地址: "/module/delete.do?id=" + id;
     * 3.响应地址: 异步响应
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(String id) {
        Map<String, String> map = new HashMap<>();
        if (moduleService.delete(id)) {
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败,请联系管理员");
        }
        return map;
    }
}
